package com.pet.server.ext.action.request;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.Option;
import com.pet.server.ext.data.UserOption;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Vote extends ActionBase{


	public Integer optionId,requestId;
	public Vote(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		optionId=NumberUtil.getInteger(params.getFieldValue("option_id"));
		requestId=NumberUtil.getInteger(params.getFieldValue("request_id"));
		if(StringUtil.isEmpty(optionId)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		UserOption uo = userService.findUserOption(user.user_id, requestId);
		Option o = userService.findOptionById(optionId);
		if(null==o||!StringUtil.equals(o.request_id, requestId)){
			throw ApiError.Type.LOGIC_ERROR.toException("投票参数错误!");
		}
		if(null!=uo){
			throw ApiError.Type.LOGIC_ERROR.toException("您已经投票!");
		}
		userService.vote(optionId, user.user_id);
		Result result = new Result();
		return result;
	}



}
